package cn.xkai.exercise.a;

import com.google.gson.Gson;

/**
 * @description: 移动零
 * 自己的思路：双指针之快慢指针交换位置，有点牵强！
 * 借鉴的思路：同理
 * @author: kaixiang
 * @date: 2022-06-29
 **/
public class Solution22 {
    public void moveZeroes(int[] nums) {
        int left = 0;
        for (int right = 0; right < nums.length; right++) {
            if (nums[right] == 0) {
                left++;
            } else if (left != 0) {
                nums[right - left] = nums[right];
                nums[right] = 0;
            }
        }
    }

    public void moveZeroesRefer(int[] nums) {
        if (nums == null || nums.length == 0) {
            return;
        }
        int index = 0;
        //一次遍历，把非零的都往前挪
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                nums[index++] = nums[i];
            }
        }
        //后面的都是0,
        while (index < nums.length) {
            nums[index++] = 0;
        }
    }

    public static void main(String[] args) {
        int[] nums = {1, 1};
        Solution22 solution22 = new Solution22();
        solution22.moveZeroes(nums);
        System.out.println(new Gson().toJson(nums));
    }
}
